package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.kennycason.kumo.WordFrequency;
import com.ruoyi.common.utils.glmApi.GlmRes;
import com.ruoyi.common.utils.glmApi.GlmUtils;
import com.ruoyi.common.utils.word.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DialogHistoryMapper;
import com.ruoyi.system.domain.DialogHistory;
import com.ruoyi.system.service.IDialogHistoryService;

/**
 * 用户问答历史Service业务层处理
 *
 * @author AousVolad
 * @date 2024-06-21
 */
@Service
public class DialogHistoryServiceImpl implements IDialogHistoryService
{
    @Autowired
    private DialogHistoryMapper dialogHistoryMapper;

    /**
     * 查询用户问答历史
     *
     * @param id 用户问答历史主键
     * @return 用户问答历史
     */
    @Override
    public DialogHistory selectDialogHistoryById(Long id)
    {
        return dialogHistoryMapper.selectDialogHistoryById(id);
    }

    /**
     * 查询用户问答历史列表
     *
     * @param dialogHistory 用户问答历史
     * @return 用户问答历史
     */
    @Override
    public List<DialogHistory> selectDialogHistoryList(DialogHistory dialogHistory)
    {
        return dialogHistoryMapper.selectDialogHistoryList(dialogHistory);
    }

    /**
     * 新增用户问答历史
     *
     * @param dialogHistory 用户问答历史
     * @return 结果
     */
    @Override
    public int insertDialogHistory(DialogHistory dialogHistory)
    {
        return dialogHistoryMapper.insertDialogHistory(dialogHistory);
    }

    /**
     * 调用GLM
     * @param dialogHistory 问题
     * @return 结果
     */

    @Override
    public DialogHistory Glm(DialogHistory dialogHistory) {
        //调用GLM
        GlmRes glmRes =  GlmUtils.glmUtil(dialogHistory.getQuestion(),"user");
        dialogHistory.setAnswer(glmRes.getChoices().getMessage().getContent());
        dialogHistoryMapper.insertDialogHistory(dialogHistory);
        return dialogHistory;
    }

    /**
     * 修改用户问答历史
     *
     * @param dialogHistory 用户问答历史
     * @return 结果
     */
    @Override
    public int updateDialogHistory(DialogHistory dialogHistory)
    {
        return dialogHistoryMapper.updateDialogHistory(dialogHistory);
    }

    /**
     * 批量删除用户问答历史
     *
     * @param ids 需要删除的用户问答历史主键
     * @return 结果
     */
    @Override
    public int deleteDialogHistoryByIds(Long[] ids)
    {
        return dialogHistoryMapper.deleteDialogHistoryByIds(ids);
    }

    /**
     * 删除用户问答历史信息
     *
     * @param id 用户问答历史主键
     * @return 结果
     */
    @Override
    public int deleteDialogHistoryById(Long id)
    {
        return dialogHistoryMapper.deleteDialogHistoryById(id);
    }
    /**
     * 获取用户问答历史信息，统计词频
     *
     * @return 词：频
     */
    @Override
    public List<WordFrequency> frequencyAnalyzer() {
        List<DialogHistory> dialogHistories = dialogHistoryMapper.selectDialogHistoryList(new DialogHistory());
        List<String> text = dialogHistories.stream().map(DialogHistory::getQuestion).collect(Collectors.toList());
        return WordUtil.wordCloud(text);
    }
}
